package com.iraeta.controller.xiangmu;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.iraeta.common.Result;
import com.iraeta.controller.BaseController;
import com.iraeta.entity.xiangmu.Gys;
import com.iraeta.service.xiangmu.GysService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController(value = "gysController")
@RequestMapping("gys")
public class GysController extends BaseController {
    @Autowired
    GysService gysService;

    @RequestMapping("pagegys")
    public String pagegys(@RequestBody JSONObject jo){
        int xmId = jo.getIntValue("xmId");
        this.pageSet(jo);
        List<Gys> gysList = gysService.listXmGys(xmId);
        PageInfo<Gys> gysPageInfo = this.getPageInfo(gysList);
        return JSON.toJSONString(Result.success(gysPageInfo));
    }

    @RequestMapping("getgys")
    public String getGys(@RequestBody JSONObject paramJb){
        int gysId = paramJb.getIntValue("id");
        Gys gys = gysService.getGys(gysId);
        return JSON.toJSONString(Result.success(gys));
    }

    @RequestMapping("addeditgys")
    public String addEditGys(@RequestBody JSONObject paramJb) throws Exception{
        int xmId = paramJb.getIntValue("id");
        Gys gys = xmId <= 0 ? new Gys() :  gysService.getGys(xmId);
        this.mapJsonToObj(paramJb, gys);
        if(xmId <= 0){
            gysService.addGys(gys);
        }else {
            gysService.updateGys(gys);
        }
        return JSON.toJSONString(Result.success());
    }

    @RequestMapping("deletegys")
    public String deleteGys(@RequestBody JSONObject paramJb){
        int gysId = paramJb.getIntValue("id");
        gysService.deleteGys(gysId);
        return JSON.toJSONString(Result.success());
    }
}
